<template>
  <div class="add-class-modal modal">
    <div class="add-class">
      <div class="header">添加班级</div>
      <div class="body">
        <div class="content-wrapper">
          <div class="row">
            <span class="label">
              <i class="red">*</i>
              年级：</span
            >
            <el-select v-model="gradeId" placeholder="请选择">
              <el-option
                v-for="{ id, name } in schoolGradeList"
                :key="id"
                :value="id"
                :label="name"
              >
              </el-option>
            </el-select>
          </div>
          <div class="row">
            <div>
              <span class="label">
                <i class="red">*</i>
                班级名称：</span
              >
              <el-input
                v-model="className"
                maxlength="10"
                show-word-limit
              ></el-input>
            </div>
            <div class="name-desc">显示为: {{ computedName }}</div>
          </div>
          <div class="row">
            <span class="label">
              <i class="red">*</i>
              班级排序：</span
            >
            <el-input v-model="classSort"></el-input>
          </div>
        </div>
      </div>
      <div class="footer">
        <div class="btn-group">
          <div class="cancel-btn btn" @click="cancel()">取消</div>
          <div class="confirm-btn btn" @click="confirm()">确认</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
export default {
  name: '',
  props: {
    initGradeId: { type: Number, required: true },
    // classTotal: { type: Number, required: true },
  },
  data() {
    return {
      gradeId: '',
      className: '',
      classSort: 1,
    };
  },
  computed: {
    ...mapGetters('school', [
      'schoolId',
      'schoolSubjectList',
      'schoolGradeList',
    ]),
    computedName() {
      const gradeName =
        this.schoolGradeList.find((e) => e.id === this.gradeId)?.name || '';
      const { className } = this;
      if (Number(className) == className) {
        return `${gradeName}（${className}）班`;
      } else {
        return `${gradeName}${className}班`;
      }
    },
  },
  watch: {
    gradeId:{
      handler(v) {
        this.getClassTotal(v);
      },
      immediate: true,
    }
  },
  methods: {
    async getClassTotal(gradeId) {
      const { F_total } = await this.$api.getClasses({
        F_school_id: this.schoolId,
        F_grade: gradeId,
        F_page: 1,
        F_limit: 10,
      });
      this.classSort = F_total + 1;
    },
    async confirm() {
      const params = {
        F_school_id: this.schoolId,
        F_grade_id: this.gradeId,
        F_class_name: this.className,
        F_class_sort: this.classSort,
      };
      const formData = new FormData();
      for (let [k, v] of Object.entries(params)) {
        formData.append(k, v);
      }
      try {
        const { F_responseNo, F_responseMsg } = await this.$api.addClass(
          formData
        );
        if (F_responseNo === 10000) {
          this.$message.success('添加班级成功');
          this.$emit('update');
        } else {
          this.$message.warning(F_responseMsg);
        }
      } catch (e) {
        console.error(e);
        const message = e?.response?.data?.F_responseMsg || '';
        this.$message.error(`添加班级失败：${message}`);  
      }
    },
    cancel() {
      this.$emit('cancel');
    },
  },
};
</script>

<style lang="less" scoped>
.add-class {
  box-sizing: border-box;
  width: 550px;
  height: 350px;
  border-radius: 6px;
  background-color: #fff;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  display: flex;
  flex-direction: column;
  .header {
    height: 60px;
    line-height: 60px;
  }
  .body {
    height: calc(100% - 120px);
    display: flex;
    flex-direction: column;
  }
  .content-wrapper {
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    padding: 10px 75px;
    text-align: left;
    .row {
      margin-bottom: 20px;
      &:last-child {
        margin: 0;
      }
      .label {
        display: inline-block;
        width: 100px;
        text-align: right;
      }
      .name-desc {
        font-size: 12px;
        padding: 5px 0 0 102px;
        color: #8c8c8c;
      }
    }
    .el-input {
      width: 230px;
    }
  }
  .red {
    font-style: normal;
    color: #f56c6c;
  }

  .footer {
    height: 60px;
    .btn-group {
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
      .btn {
        width: 90px;
        height: 35px;
        line-height: 35px;
      }
      .cancel-btn {
        background-color: #ffffff;
        color: #b3b3b3;
        border: solid 1px #f5f5f5;
        margin-right: 20px;
      }
      .confirm-btn {
        background-color: #009cff;
        color: #fff;
      }
    }
  }
}
</style>
